package com.tjh.utils;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

public abstract class ImgUtils {
    /**
     * 添加文字水印的方法
     * @param pressText 要添加的文字
     * @param path 文件路径，这里只处理了了本地文件，未处理网络图片
     * @param x 文字在图片的横坐标，左上角为0，往右递增
     * @param y 文字在图片的纵坐标，左上角为0，往下递增
     * @param fontName 文字的字体名称
     * @param fontSize 文字的大小
     * @param fontColor 文字的眼神
     * @param style 文字的格式，如Font.BOLD++Font.ITALIC表示粗体斜体
     * @return
     */
    public static byte[] generateText(String pressText, String path, int x, int y, String fontName,
                                      int fontSize, Color fontColor, int style){
        try {
            BufferedImage bgImage= ImageIO.read(new File(path));
            int wideth = bgImage.getWidth(null);
            int height = bgImage.getHeight(null);
            BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(bgImage, 0, 0, wideth, height, null);

            //设置字体大小、颜色等
            g.setColor(fontColor);
            g.setFont(new Font(fontName, style, fontSize));

            g.drawString(pressText, x, y);
            g.dispose();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "png", out);
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 为背景图片增加水印图片
     *
     * @param path          水印图片，本地地址，未做网络图片转换
     * @param bgPath        背景图片，同上
     * @param x              横坐标,左上角0，往右递增
     * @param y              纵坐标,左上角0，往下递增
     * @param width          水印图片宽度
     * @param height         水印图片高度
     * @return
     */
    public static byte[] generateImage(String path,String bgPath , int x, int y, int width, int height) {
        try {
            BufferedImage watermarkImage = ImageIO.read(new File(path));
            BufferedImage bgImage = ImageIO.read(new File(bgPath));
            int bgWidth = bgImage.getWidth(null);
            int bgHeight = bgImage.getHeight(null);
            BufferedImage image = new BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(bgImage, 0, 0, bgWidth, bgHeight, null);
            width = width == 0 ? watermarkImage.getWidth() : width;
            height = height == 0 ? watermarkImage.getHeight() : height;
            g.drawImage(watermarkImage, x, y, width, height, null);
            g.dispose();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", out);
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 将字节数组转换为本地图片
     * @param data
     * @param path
     */
    public static void byte2image(byte[] data, String path) {
        if (data.length < 3 || path.equals("")){
            return;
        }
        try {
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
